/**
 * SpringMVC restful 风格
 * 合作伙伴控制器，控制关于合作伙伴的一些业务逻辑操作
 * @author yanwl
 * @date 2014-08-20
 */
package com.chnbst.bstyun.controller.user;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.model.pojo.Area;
import com.chnbst.bstyun.model.pojo.City;
import com.chnbst.bstyun.model.pojo.Menu;
import com.chnbst.bstyun.model.pojo.Province;
import com.chnbst.bstyun.service.MenuService;
import com.chnbst.bstyun.service.PlaceService;
import com.chnbst.bstyun.service.UserService;
import com.mysql.jdbc.StringUtils;

@Controller
@RequestMapping("/partner")
public class PartnerController {
	
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private MenuService menuService;
	@Autowired
	private UserService userService;
	
	@Autowired
	private PlaceService placeService;
	private JsonGenerator jsonGenerator = null;
	private ObjectMapper objectMapper = new ObjectMapper();
	
	/**
	 * http://xxx.com/partner
	 * get请求 返回合作伙伴首页
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView cooperater(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/joinus");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/apply
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到伙伴申请页面
	 */
	@RequestMapping(value="/apply",method=RequestMethod.GET)
	public ModelAndView apply(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		mav.setViewName("cooperater/hbsq");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/apply/commit
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到伙伴申请页面
	 */
	@RequestMapping(value="/apply/commit",method=RequestMethod.GET)
	public ModelAndView commit(HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		
		mav.setViewName("cooperater/qrtj");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/developer
	 * get请求 返回软件开发商页面
	 */
	@RequestMapping(value="/developer",method=RequestMethod.GET)
	public ModelAndView partner(){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/partner");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/developer/kfsxq
	 * get请求 返回开发商详情页面
	 */
	@RequestMapping(value="/developer/kfsxq",method=RequestMethod.GET)
	public ModelAndView kfsxq(){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/kfsxq");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/developer/kfsxq/fsyj
	 * get请求 返回给开发商发送邮件页面
	 */
	@RequestMapping(value="/developer/kfsxq/fsyj",method=RequestMethod.GET)
	public ModelAndView fsyj(){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/fsyj");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/qddls
	 * get请求 返回渠道代理商页面
	 */
	@RequestMapping(value="/qddls",method=RequestMethod.GET)
	public ModelAndView qddls(){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		/**
		 * 地区联动相关====================
		 */
		
		List<Province> provinceList = placeService.getProvinceList();
		mav.addObject("provinceList",provinceList);
		/**
		 * 地区联动相关====================
		 */
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/qddls");
		return mav;
	}
	
	@RequestMapping(value="/citys",method=RequestMethod.GET)
	public @ResponseBody String getCityByPcode(HttpServletRequest request) throws JsonProcessingException, IOException{
		String provinceCode = request.getParameter("provinceCode");
		if(StringUtils.isEmptyOrWhitespaceOnly(provinceCode)){
			return null;
		}
		List<City> cityList = placeService.getCityByPcode(Integer.parseInt(provinceCode));
		String cityInfos = "";
		if(cityList.size()>0){
			cityInfos = objectMapper.writeValueAsString(cityList);
		}
		return cityInfos;
	}
	
	@RequestMapping(value="/areas",method=RequestMethod.GET)
	public @ResponseBody String getAreaByCcode(HttpServletRequest request) throws JsonProcessingException, IOException{
		String cityCode = request.getParameter("cityCode");
		if(StringUtils.isEmptyOrWhitespaceOnly(cityCode)){
			return null;
		}
		List<Area> areaList = placeService.getAreaByCcode(Integer.parseInt(cityCode));
		String areaInfos = "";
		if(areaList.size()>0){
			areaInfos = objectMapper.writeValueAsString(areaList);
		}
		return areaInfos;
	}
	
	/**
	 * http://xxx.com/partner/dlsjoin
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到代理商加盟页面
	 */
	@RequestMapping(value="/dlsjoin",method=RequestMethod.GET)
	public ModelAndView dlsjoin(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/dlsjoin");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/cityzdjoin
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到市总代加盟页面
	 */
	@RequestMapping(value="/cityzdjoin",method=RequestMethod.GET)
	public ModelAndView citydjoin(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/cityzdjoin");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/prozdjoin
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到省总代加盟页面
	 */
	@RequestMapping(value="/prozdjoin",method=RequestMethod.GET)
	public ModelAndView prozdjoin(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/prozdjoin");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/rjkfjoin
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到软件开发商加盟页面
	 */
	@RequestMapping(value="/rjkfjoin",method=RequestMethod.GET)
	public ModelAndView rjkfjoin(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/rjkfjoin");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/ggsjoin
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到广告商加盟页面
	 */
	@RequestMapping(value="/ggsjoin",method=RequestMethod.GET)
	public ModelAndView ggsjoin(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/ggsjoin");
		return mav;
	}
	
	/**
	 * http://xxx.com/partner/oemjoin
	 * get请求 如果该用户没有登录直接返回到登录界面，否则返回到OEM加盟页面
	 */
	@RequestMapping(value="/oemjoin",method=RequestMethod.GET)
	public ModelAndView oemjoin(HttpServletRequest request){
		List<Menu> menus = menuService.selectMenus();
		ModelAndView mav = new ModelAndView();
		
		mav.addObject("menus", menus);
		mav.setViewName("cooperater/oemjoin");
		return mav;
	}
	
	
}
